Sleep:
	call SafeInterruptDI
	push af
			jp c,_
		call ClearBuffer
		call FastCopy
		call LCDDelay ; Just to be safe
_		ld a, 2
		out (10h), a ; Disable LCD
		di ; And interrupts, for now
		im 1 ; interrupt mode 1, for cleanliness
		ei ; Enable interrupting when ON is pressed
		ld a, 1h
		out (03h), a ; ON
		halt ; and halt :)
		di
		ld a,INTERRUPT_MASK ; Reset the interrupts
		out (03h), a
		ld a, 3
		out (10h), a ; Enable the screen
	pop af
	ret c
	ei
	ret
SafeInterruptDI:
	xor a		;Set nc (interrupts)
	push af		;Check interrupts
	pop af
	ld a,i		;pe if interrupt
	di
		ret pe		;Return if interrupts on
	dec sp		;Otherwise, robust test
	dec sp
	pop af
	or a		;Set nc (interrupts)
		ret nz		;Return if interrupts on
	scf			;Set c (no interrupts)
	ret			;Return
CPDEBC:
	push de
	ex de, hl
	or a
	sbc hl, bc
	ex de, hl
	pop de
	ret
DEMulA:          ; HL = DE × A
    LD     HL, 0      ; Use HL to store the product
    LD     B, 8       ; Eight bits to check
_loop:
    RRCA             ; Check least-significant bit of accumulator
    JR     NC, _skip  ; If zero, skip addition
    ADD    HL, DE
_skip:
    SLA    E         ; Shift DE one bit left
    RL     D
    DJNZ   _loop
    RET
; divide
; inputs	hl/de
; outputs	hl
Div16by16:
	ld	a,h
	ld	c,l
	ld	hl,0
	ld	b,16
__Div16by16_Loop:
	scf
	rl	c
	rla
	adc	hl,hl
	sbc	hl,de
	jr	nc,__Div16by16_Skip
	add	hl,de
	dec	c
__Div16by16_Skip:
	djnz	__Div16by16_Loop
	ld	h,a
	ld	l,c
	ret
; Input: HL and DE are strings
; Output: Z if equal, NZ otherwise
CmpStrings:
	push hl
		push de
_				ld a, (de)
				or a
					jr z, _
				cp (hl)
					jr nz,++_
				inc hl
				inc de
				jr -_
_			ld a, (hl)
			or a
_		pop de
	pop hl
	ret
; converts hex in hl to ascii string
; input		HL   = number to convert
;		DE   = location of ASCII string
; output	(de) = ascii string 
Num2Dec:
	ld	bc,-10000
	call	Num1
	ld	bc,-1000
	call	Num1
	ld	bc,-100
	call	Num1
	ld	c,-10
	call	Num1
	ld	c,b
Num1	ld	a,'0'-1
Num2	inc	a
	add	hl,bc
	jr	c,Num2
	sbc	hl,bc

	ld	(de),a
	inc	de
	ret
